package org.example.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.commonapi.domain.Inbox;
import org.example.commonapi.domain.R;
import org.example.commonapi.dto.LoginInfo;
import org.example.commonapi.dto.PageInfo;
import org.example.commonapi.util.JwtUtil;
import org.example.commonapi.util.options.InboxService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class InboxController {

    @Resource
    private InboxService inboxService;

    @Resource
    private JwtUtil jwtUtil;

    /**
     * 获取收件箱信息
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    @GetMapping("/list/inbox")
    public R listInbox(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, HttpServletRequest request, Integer type) {
        LoginInfo loginInfo=jwtUtil.parseJwt(request.getHeader("token"));
//        条件
        Page<Inbox> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Inbox> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Inbox::getUserId,loginInfo.getId());
//        获取条件
        if(type!=null)queryWrapper.eq(Inbox::getType,type);
        IPage<Inbox> list=inboxService.page(page,queryWrapper);
        PageInfo pageInfo=new PageInfo(list);
        return R.success(pageInfo);
    }


}
